package com.zsyh.jiujiu.fragment.vipFragment;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyInfoBeeean;
import com.zsyh.jiujiu.bean.TaoCanBean;
import com.zsyh.jiujiu.bean.WEiXInPayBean;
import com.zsyh.jiujiu.bean.ZhuCeBeaen;
import com.zsyh.jiujiu.home.MyOrderActivity;
import com.zsyh.jiujiu.home.MyvipActtivitty;
import com.zsyh.jiujiu.net.MyFragementObserver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MonthFragment extends BaseFragment {

    @BindView(R.id.dengji)
    TextView dengji;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.shengyijici)
    TextView shengyijici;
    @BindView(R.id.youxiaoqi)
    TextView youxiaoqi;
    @BindView(R.id.re_dengji)
    RelativeLayout reDengji;
    @BindView(R.id.tv_dengji)
    TextView tvDengji;
    @BindView(R.id.rvlist)
    RecyclerView rvlist;
    @BindView(R.id.buy)
    TextView buy;
    private IWXAPI api;
    private String dangqian;

    private BaseQuickAdapter<ZhuCeBeaen, BaseViewHolder> mAdapter;
    List<ZhuCeBeaen> list = new ArrayList<>();
    Context context;
    public MonthFragment(Context context){
        this.context=context;
    }


    @Override
    protected int initLayout() {
        return R.layout.fragment_month;
    }

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {
        api= WXAPIFactory.createWXAPI((MyvipActtivitty)getActivity(),null);
        ZhuCeBeaen one = new ZhuCeBeaen();
        one.setName("发布产品");
        one.setDesc("免费发布产品");
        one.setImage(R.mipmap.ic_baoyue_yi);
        list.add(one);

        ZhuCeBeaen two = new ZhuCeBeaen();
        two.setName("信息匹配");
        two.setDesc("信息推送被动成交");
        two.setImage(R.mipmap.ic_baoyue_er);
        list.add(two);

        ZhuCeBeaen three = new ZhuCeBeaen();
        three.setName("联系客户");
        three.setDesc("免费10次联系客户");
        three.setImage(R.mipmap.ic_baoyue_san);
        list.add(three);

        ZhuCeBeaen four = new ZhuCeBeaen();
        four.setName("无受限交易");
        four.setDesc("不限制交易额度");
        four.setImage(R.mipmap.ic_baoyue_si);
        list.add(four);

        ZhuCeBeaen five = new ZhuCeBeaen();
        five.setName("在线客服");
        five.setDesc("24小时客服服务");
        five.setImage(R.mipmap.ic_baoyue_wu);
        list.add(five);

        ZhuCeBeaen six = new ZhuCeBeaen();
        six.setName("意向客户");
        six.setDesc("与其他会员直接交易");
        six.setImage(R.mipmap.ic_baoyue_liu);
        list.add(six);

        ZhuCeBeaen qi = new ZhuCeBeaen();
        qi.setName("免费交易");
        qi.setDesc("有效期间免费交易");
        qi.setImage(R.mipmap.ic_baoyue_qi);
        list.add(qi);

        mAdapter = new BaseQuickAdapter<ZhuCeBeaen, BaseViewHolder>(R.layout.item_zhuce, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, ZhuCeBeaen item) {
                helper.setText(R.id.name, item.getName());
                helper.setText(R.id.desc, item.getDesc());
                helper.setImageResource(R.id.imagezhuce, item.getImage());
            }
        };

        rvlist.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvlist.setAdapter(mAdapter);


    }

    /**
     * getuser
     */
    public void gettUser(String userid) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getUserById(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("Myinfo", JsonStr + "");
                        Gson gson = new Gson();
                        MyInfoBeeean myInfoBeeean = gson.fromJson(JsonStr, MyInfoBeeean.class);
                        dangqian=myInfoBeeean.getData().getLevel();
                        if (myInfoBeeean.getData().getLevel().equals("834598172247461888")) {
                            dengji.setText("包月会员");
                            buy.setVisibility(View.VISIBLE);
                        } else if (myInfoBeeean.getData().getLevel().equals("834598284730306560")) {
                            dengji.setText("当前等级");
                            if(EmptyUtil.isEmpty(myInfoBeeean.getData().getEndTime())){
                                buy.setVisibility(View.VISIBLE);
                            }else {
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                                String riqi = df.format(new Date());
                                if(compareDate(riqi,myInfoBeeean.getData().getEndTime())==true){
                                    buy.setVisibility(View.GONE);
                                }else{
                                    buy.setVisibility(View.VISIBLE);
                                }
                            }
                        } else if (myInfoBeeean.getData().getLevel().equals("834598356696174592")) {
                            dengji.setText("包月会员");
                            if(EmptyUtil.isEmpty(myInfoBeeean.getData().getEndTime())){
                                buy.setVisibility(View.VISIBLE);
                            }else {
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                                String riqi = df.format(new Date());
                                if(compareDate(riqi,myInfoBeeean.getData().getEndTime())==true){
                                    buy.setVisibility(View.GONE);
                                }else{
                                    buy.setVisibility(View.VISIBLE);
                                }
                            }
                        }

                        dengji((String) SPUtils.get(getActivity(), "userid", ""), "834598284730306560");
                    }
                });
    }

    public boolean compareDate(String nowDate, String compareDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date now = df.parse(nowDate);
            Date compare = df.parse(compareDate);
            if (now.before(compare)) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        gettUser((String) SPUtils.get(getActivity(), "userid", ""));
    }

    /**
     * 等级
     */
    public void dengji(String userid, String id) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .userHonesty(userid, id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("dengji", JsonStr + "");
                        Gson gson = new Gson();
                        TaoCanBean taoCanBean = gson.fromJson(JsonStr, TaoCanBean.class);
                        buy.setText("¥ "+taoCanBean.getData().getNeedMoney()+" 立即升级");
                        if (dangqian.equals("834598172247461888")) {
                            money.setText("¥"+taoCanBean.getData().getNeedMoney()+" 包月会员");
                            shengyijici.setText("联系对方："+taoCanBean.getData().getPhoneTimes()+"次");
                            youxiaoqi.setVisibility(View.GONE);
                        } else if (dangqian.equals("834598284730306560")) {


                            if(EmptyUtil.isEmpty(taoCanBean.getData().getEndTime())){
                                money.setText("¥"+taoCanBean.getData().getNeedMoney()+" 包月会员");
                                shengyijici.setText("联系对方："+taoCanBean.getData().getPhoneTimes()+"次");
                                youxiaoqi.setVisibility(View.GONE);
                            }else {
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                                String riqi = df.format(new Date());
                                if(compareDate(riqi,taoCanBean.getData().getEndTime())==true){
                                    money.setText("¥"+taoCanBean.getData().getNeedMoney()+" 包月会员");
                                    shengyijici.setText("联系对方剩余："+taoCanBean.getData().getViewTimes()+"次");
                                    youxiaoqi.setText("有效期："+taoCanBean.getData().getEndTime());
                                }else{
                                    money.setText("¥"+taoCanBean.getData().getNeedMoney()+" 包月会员");
                                    shengyijici.setText("联系对方："+taoCanBean.getData().getPhoneTimes()+"次");
                                    youxiaoqi.setVisibility(View.GONE);
                                }
                            }

                        } else if (dangqian.equals("834598356696174592")) {
                            money.setText("¥"+taoCanBean.getData().getNeedMoney()+" 包月会员");
                            shengyijici.setText("联系对方："+taoCanBean.getData().getPhoneTimes()+"次");
                            youxiaoqi.setVisibility(View.GONE);
                        }

                    }
                });
    }

    @OnClick(R.id.buy)
    public void onViewClicked() {
        buy("834598284730306560",(String) SPUtils.get(getActivity(), "userid", ""));

    }


    /**
     * 构面
     */
    public void buy(String id,String userid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .wxPayVip(id, userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Gson gson=new Gson();
                        WEiXInPayBean wEiXInPayBean=gson.fromJson(JsonStr,WEiXInPayBean.class);
                        api.registerApp(wEiXInPayBean.getData().getAppid());
                        PayReq request = new PayReq();
                        request.appId= wEiXInPayBean.getData().getAppid();
                        request.partnerId= wEiXInPayBean.getData().getPartnerid();
                        request.prepayId= wEiXInPayBean.getData().getPrepayid();
                        request.nonceStr= wEiXInPayBean.getData().getNoncestr();
                        request.timeStamp= wEiXInPayBean.getData().getTimestamp();
                        request.packageValue= "Sign=WXPay";
                        request.sign= wEiXInPayBean.getData().getSign();
                        request.extData= "app data"; // optional
                        api.sendReq(request);
                    }
                });
    }

}
